#include "snake.h"
#include "ui_snake.h"
#include<QPainter>
#include<QKeyEvent>
Snake::Snake(QWidget *parent)
    : QMainWindow(parent),
    ui(new Ui::Snake),blsRun(false),Speed(500)
{
    ui->setupUi(this);
    this->setGeometry(QRect(600,300,290,310));
}

Snake::~Snake()
{
    delete ui;
}

void Snake::paintEvent(QPaintEvent *event){
  QPainter painter(this);
  if(!blsRun){
      InitSnake();
  }
  //画游戏背景
  painter.setPen(Qt::black);
  painter.setBrush(Qt::gray);
  painter.drawRect(15,15,260,260);

  painter.setPen(Qt::black);
  painter.setBrush(Qt::black);
  painter.drawRect(20,20,250,250);
 painter.drawPixmap(20,20,250,250,QPixmap("D:/Snake/img/bg.jpg"));
  //画格子
 /*painter.setPen(Qt::white);
  for(int i=2;i<=27;i++){
      painter.drawLine(20,i*10,270,i*10);
      painter.drawLine(i*10,20,i*10,270);
  }*/

  //显示游戏开始和结束
 QFont font1("Courier",24);
 painter.setFont(font1);
 painter.setPen(Qt::red);
 painter.setBrush(Qt::red);
 painter.drawText(40,150,Display);

  //显示分数
 QFont font2("Courier",15);
 painter.setFont(font2);
 painter.setPen(Qt::blue);
 painter.setBrush(Qt::blue);
 painter.drawText(140,300,"得分：");
 painter.drawText(230,300,QString::number(nScore));


  //画蛇
  painter.setPen(Qt::black);
  painter.setBrush(Qt::green);
  painter.drawPixmap(Food,QPixmap("D:/Snake/img/flower.jpg"));
 // painter.drawRect(Food);
  painter.drawRects(&vSnakeRect[0],vSnakeRect.size());
  if(blsover)
      timer->stop();//计时器停止。不再调用更新函数
}
void Snake::InitSnake(){
    nDirection=2;//默认向下运动
    blsRun=true;
    blsover=false;
    Display="游戏开始！";
    nScore=0;
    Food=CreateFood();
    //QRect rect(100,70,10,10);
   // vSnakeRect=rect;//将矩形赋值给蛇
    vSnakeRect.resize(5);//蛇的长度
    for(int i=0;i<vSnakeRect.size();i++)
    {
        QRect rect(100,70+10*i,10,10);//设定小方块
        vSnakeRect[vSnakeRect.size()-1-i]=rect;//小方块赋值到容器中
    }
    SnakeHead=vSnakeRect.first();
    timer=new QTimer(this);//设定计时器
    timer->start(Speed);//计时器时间为500ms
    connect(timer,SIGNAL(timeout()),SLOT(Snake_update()));
}
void Snake:: Snake_update(){
    Display=" ";
    SnakeHead=vSnakeRect.first();
    IsEat();
    IsHit();
    for(int j=0;j<vSnakeRect.size()-1;j++){
        vSnakeRect[vSnakeRect.size()-1-j]=vSnakeRect[vSnakeRect.size()-2-j];
    }
    switch(nDirection)
    {
    case 1:
        SnakeHead.setTop(SnakeHead.top()-10);
        SnakeHead.setBottom(SnakeHead.bottom()-10);
        break;
    case 2:
        SnakeHead.setTop(SnakeHead.top()+10);
        SnakeHead.setBottom(SnakeHead.bottom()+10);
        break;
    case 3:
        SnakeHead.setLeft(SnakeHead.left()-10);
        SnakeHead.setRight(SnakeHead.right()-10);
        break;
    case 4:
        SnakeHead.setLeft(SnakeHead.left()+10);
        SnakeHead.setRight(SnakeHead.right()+10);
        break;
    default:;

    }
    vSnakeRect[0]=SnakeHead;
    if(SnakeHead.left()<20||SnakeHead.right()>270||SnakeHead.top()<20||SnakeHead.bottom()>270){
        blsover=true;
        Display="游戏结束！";
    }
    update();//paintevent更新
}
void Snake::keyPressEvent(QKeyEvent *event){
    QKeyEvent *key=(QKeyEvent*)event;
    switch(key->key()){
    case Qt::Key_Up:nDirection=1;
        break;
    case Qt::Key_Down:nDirection=2;
        break;
    case Qt::Key_Left:nDirection=3;
        break;
    case Qt::Key_Right:nDirection=4;
        break;
    default:;
    }
    update();//paintEvent更新
}
QRect Snake::CreateFood(){
    int x,y;
    x=rand()%25;
    y=rand()%25;
    QRect rect(20+x*10,20+y*10,10,10);//食物小方块的位置
    return rect;

}
void Snake::IsEat(){
    if(SnakeHead==Food){
        SnakeHead=Food;
        vSnakeRect.push_back(vSnakeRect.last());//最后一个点加进容器
        Food=CreateFood();//再生成新食物
        nScore+=10;
        Speed=Speed-20;
        timer->stop();//停止当前计时器
        timer->start(Speed);
    }

}

void Snake::IsHit(){
    for(int i=1;i<vSnakeRect.size();i++){
        if(SnakeHead==vSnakeRect[i]){
            Display="游戏结束！";
            blsover=true;
           update();
        }
    }

}
